<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员标签
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'标签列表'),
			array('op'=>'edit','title'=>'添加标签'),
);
//会员标签
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_member_group_tag',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['usernum']=pdo_count('jy_scrm_member',array('merchid'=>$_SESSION['onemerchid'],'groupid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['qunnum']=0;
		}
	}	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'标签名称','type'=>'string'),
			array('name'=>'groupid','title'=>'客户组','type'=>'select','options'=>'groups'),
			array('name'=>'usernum','title'=>'客户数','type'=>'string'),
			array('name'=>'qunnum','title'=>'客户群数','type'=>'string'),
			//array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_scrm/member.tags/uptags/','title'=>'同步标签'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'标签名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'status2'),
		)
	);
	$return['htmls']=$htmls;
	$return['groups']=$this->getall('member_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_member_group_tag',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回标签列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'标签名称','name'=>'title','help'=>''),
					array('type'=>'select','title'=>'用户组','name'=>'groupid','options'=>'groups','help'=>''),
					array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),					
				)
			),
		)
	);
	$return['groups']=$this->getall('member_group',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['groupid']){
		exi('请选择用户组',true);
	}
	if(!$postdata['title']){
		exi('请填写标签名称',true);
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'status'=>trim($postdata['status']),
		'groupid'=>trim($postdata['groupid']),
	);
	$id=intval($postdata['id']);
	if($id){
		$post=array(
			'id'=>$postdata['tag_id'],
			'name'=>$postdata['title'],
		);
		model('core/wxwork_userapi')->edit_member_tags($post);
		pdo_update('jy_scrm_member_group_tag',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$group_id=pdo_getcolumn('jy_scrm_member_group',array('id'=>$data['groupid'],'uniacid'=>$_SESSION['uniacid']),'group_id');
		$tag[0]=array('name'=>$data['title']);
		$post=array(
			'group_id'=>$group_id,
			'tag'=>$tag,
		);
		$res=model('core/wxwork_userapi')->add_member_tags($post);
		$data['tag_id']=$res['tag'][0]['id'];
		pdo_insert('jy_scrm_member_group_tag',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择用户组！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_scrm_member_group_tag',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='uptags'){
	$this->model('member')->up_tags();
	exi('同步标签成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	$tag_ids=array();
	foreach($del as $id){
		$tag_ids[]=pdo_getcolumn('jy_scrm_member_group_tag',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'tag_id');
	}
	$post=array(
		'tag_id'=>$tag_ids,
	);
	model('core/wxwork_userapi')->del_member_tags($post);
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_member_group_tag',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}